<?php

namespace Api\Controller;

use Common\Controller\ApiController;

/**
 * 照片管理API接口
 */
class PhotoController extends ApiController {

    /**
     * 上传照片
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-28
     */
    public function uploadPhoto() {
        //图片后缀默认为jpg
        $ext = I('post.ext', 'jpg');
        $file = I('post.file', '');
        $desc = I('post.desc', '');
        $userid = I('post.userid', '');
        if (empty($file)) {
            apiReturn('无图片', 300);
        }
        if (empty($userid)) {
            apiReturn('无上传人', 300);
        }
        if (empty($desc)) {
            apiReturn('描述不能为空', 300);
        }
        //获取图片的二进制流
        $pic_binary = base64_decode($file);
        //图片存储路径
        $up = "./Uploads/photo/";
        $path = realpath($up);
        //创建或进入目录，linux上
        if(!file_exists($path)){
            mkdir($path);
        }
        //图片命名
        $pic_name = date('YmdHis') . "_" . $userid . "." . $ext;
        $file_name = $path . "/" . $pic_name;
        //生成上传图片
        $info = createPic($file_name, $pic_binary);
        if (!$info) {
            apiReturn('上传失败,图片未上传', 300);
        } else {
            $ali_url = $up . $pic_name;
            if (aliossUploadOne($ali_url)) {
                $src = getImgUrl($ali_url);
                $rs = M('Users')->field('u.id,ug.group_id')
                        ->where("u.id={$userid}")
                        ->alias('u')
                        ->join('LEFT JOIN __USER_GROUP_ACCESS__ ug ON u.id=ug.user_id')
                        ->find();
                if (!$rs) {
                    apiReturn('上传人不存在', 300);
                }
                if ($rs['group_id'] != 2) {
                    apiReturn('非专业人员无法上传', 300);
                }
                $data = array(
                    'src' => $src,
                    'desc' => $desc,
                    'user_id' => $userid,
                    'add_time' => time(),
                );

                $ret = M('Photo')->add($data);
                if ($ret) {
                    apiReturn('上传成功', 200);
                } else {
                    apiReturn('上传失败', 300);
                }
            } else {
                apiReturn('上传失败,图片上传服务器失败', 300);
            }
        }
    }

    /**
     * 获取照片列表
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-28
     */
    public function photoList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'add_time');
        $orderDirection = I('post.orderDirection', 'DESC');
        $total = M('Photo')->count();
        $list = M('Photo')->field('id,src,desc')->order("{$orderField} {$orderDirection}")->page("{$pageCurrent},{$pageSize}")->select();
        if (!empty($list)) {
            $data['total'] = $total;
            $data['pages'] = ceil($total / $pageSize);
            $data['list'] = $list;
            apiReturn('获取数据', 200, $data);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取照片详情
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-27
     */
    public function photoInfo() {
        $pid = I('post.pid', 0);
        if (empty($pid)) {
            apiReturn('系统错误，无图片', 300);
        }
        $model = M('Photo');
        $rs = $model->find($pid);
        if ($rs) {
            apiReturn('图片详情', 200, $rs);
        } else {
            apiReturn('无数据', 300);
        }
    }

}
